package com.mvp.shq.washer.ui.shop.shopAddress;

import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.entity.shop.AddressData;
import com.mvp.shq.washer.entity.shop.result.AddressResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.http.RxSchedulers;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;

import java.util.List;

/**
 * Created by Administrator on 2017/10/10.
 */

public class Presenter extends ShopAddressConstract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    void getAllAddress(int pageNo, int pageSize) {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        model.getAllAddress(token,userId,pageNo,pageSize)
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<AddressResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<AddressResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getAllAddressS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void setDefAddress(String ecReceiveAddressId) {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        model.setDefAddress(token,userId,ecReceiveAddressId)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.setDefAddressS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void delAddress(String ecReceiveAddressId) {
        String token = SpUtil.getToken();
        model.delAddress(token,ecReceiveAddressId)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.delAddressS();
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }
}
